package org.consoleTesting;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class UserFileStatistics {
	public final static String RootPath = "statistics/";

	public static String getUserStatistic(String userName) {
		StringBuilder statistics = new StringBuilder();
		try {
  			Scanner file_scan = new Scanner(new FileReader(UserFileStatistics.RootPath + userName));
			while(file_scan.hasNext()){	
			statistics.append(file_scan.nextLine()).append("\n");
			}
		} catch(Exception e) {
			return "have no statistics yet\n";
		}
	
		return statistics.toString();
	}
	public static void writeStatistics(double wholeResults, String userName, int numQuestions) {
		FileWriter wr = null;
		try {
			wr = new FileWriter(UserFileStatistics.RootPath + userName,
					true);
		} catch (IOException e) {
			System.err.println("something wrong with openig file for statistics, exiting");
			System.exit(0);
		}
		Calendar cal = new GregorianCalendar();
		try {
			wr.write(String.format("%s test finished; score is %.2f of %d %n ",new SimpleDateFormat().format(cal.getTime())
					,wholeResults, numQuestions ));
			wr.flush();
		} catch(IOException e) {
			System.err.println("Error writing to file, exiting");
			System.exit(0);
			
		}
	}

}
